﻿using SHDocVw;
using System;
using System.Threading;
using System.Windows.Forms;

namespace FHZGTools
{
    public partial class Form1 : Form
    {
        private Dialog dl;
        private Boolean showDl=false;
        private int count = 0;
        private Boolean isTiming = false;
        private HtmlDocument htmlDoc;
        delegate void SetTextBoxCallback(string str); //定义委托
        delegate void SetLabelCallback(string str); //定义委托
        delegate void GetWebBrowser(); //定义委托
        delegate void CheckBeAtkCallback(); //定义委托
        delegate void OpenPsCallback(); //定义委托
        delegate void ItemClickCallback(); //定义委托
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.ShowInTaskbar = false;
            dl = new Dialog();
            wb.Navigate("http://game.zg.qq.com/");
            wb.ScriptErrorsSuppressed = true;
            
        }
        public void SetTime(string str){
            if (tb_output.InvokeRequired){
                SetLabelCallback setListCallback = new SetLabelCallback(SetTime); //实例化委托对象
                lb_t.Invoke(setListCallback, str); //重新调用SetListBox函数
            }else{
                lb_t.Text = str;
            }
        }
        public void log(string str){
            if (tb_output.InvokeRequired){
                SetTextBoxCallback setListCallback = new SetTextBoxCallback(log); //实例化委托对象
                tb_output.Invoke(setListCallback, str); //重新调用SetListBox函数
            }else{
                tb_output.Text+=str+"\r\n";
            }
        }
        private void getHtml(){
            if (tb_output.InvokeRequired){
                CheckBeAtkCallback gwb = new CheckBeAtkCallback(CheckBeAtk); //实例化委托对象
                wb.Invoke(gwb); //重新调用SetListBox函数
            }else {
                htmlDoc = wb.Document;
            }
        }
        public void CheckBeAtk(){
            getHtml();
            string str = htmlDoc.Body.InnerHtml;
            if (str.IndexOf("遭到攻击") != -1){
                log("有敌方对我城池进行攻击！");
                showDialog("有敌方对我城池进行攻击！");
            }
        }
        private void getPrice(int type){
            string name = (type == 8 ? "8|九鼎|" : "9|和氏璧|");
            try{
                getHtml();
                htmlDoc = htmlDoc.Window.Frames["CashCenterDiv"].Document;
                string str = htmlDoc.Body.InnerHtml;
                string tmp = str;
                int max = 8000;
                int min = 12000;
                int cur = 0;
                while (tmp.IndexOf(name) != -1)
                {
                    tmp = tmp.Substring(tmp.IndexOf(name)+name.Length);
                    int st = tmp.IndexOf("|") + 1;
                    int l = tmp.IndexOf(" ") - tmp.IndexOf("|") - 1;
                    cur = Convert.ToInt32(tmp.Substring(st, l));
                    if (cur > max) max = cur;
                    if (cur < min) min = cur;
                }
                log((type == 8 ? "九鼎" : "和氏璧") + "当前最低价【" + min + "】");
                log((type == 8 ? "九鼎" : "和氏璧") + "当前最高价【" + max + "】");
            }catch(Exception err) { }
        }
        private void checkPs(){
            DateTime DT = System.DateTime.Now;
            string dt = System.DateTime.Now.ToString();
            log(dt);
            getPrice(8);
            getPrice(9);
        }
        private void showDialog(string str){
            if (dl.IsDisposed){
                dl = new Dialog();
                showDl = false;
            }
            if (showDl==true){
                dl.Activate();
                return;
            }
            showDl = true;
            dl.showTip(str);
            dl.ShowDialog();
        }
        private void tb_output_KeyDown(object sender, KeyEventArgs e){
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A){
                ((TextBox)sender).SelectAll();
            }
        }
        private void button1_Click(object sender, EventArgs e){
            timing();
        }
        private void btn_show_Click(object sender, EventArgs e)
        {
            wb.Visible = !wb.Visible;
            btn_show.Text = (wb.Visible ? "隐藏" : "显示");
        }

        private void tb_output_TextChanged(object sender, EventArgs e)
        {
            tb_output.SelectionStart = tb_output.Text.Length;
            tb_output.ScrollToCaret();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            CheckBeAtk();
            checkPs();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            openFb();
            //openActive();
        }
        private void openFb()
        {
            getHtml();
            htmlDoc=htmlDoc.GetElementById("rightside").Document;
            htmlDoc.GetElementById("shortcut").Children[6].InvokeMember("click");
            Thread t = new Thread(new ThreadStart(openKhd));
            t.Start();
        }
        private void openKhd(){
            Thread.Sleep(500);
            if (wb.InvokeRequired){
                OpenPsCallback setListCallback = new OpenPsCallback(openKhd); //实例化委托对象
                wb.Invoke(setListCallback); //重新调用SetListBox函数
            }else {
                getHtml();
                htmlDoc = wb.Document.Window.Frames["copydiv"].Document;
                HtmlElement btn = htmlDoc.All["copy_index_copy1_enterbtn"];
                btn.InvokeMember("click");
                btn = htmlDoc.All["copy_war1_main_list"];
                HtmlDocument hd = btn.Children[4].Document;
                btn = hd.All["copy_war_list_btnBtn_4"];
                btn.Children[0].InvokeMember("click");
                btn = htmlDoc.All["checkType"];
                btn.Children[2].Children[1].InvokeMember("click");
                btn= htmlDoc.All["showCopy5Go"];
                btn.InvokeMember("click");
            }
        }
        private void openActive()
        {
            getHtml();
            htmlDoc.GetElementById("menu").Children[7].InvokeMember("click");
            Thread t = new Thread(new ThreadStart(openPs));
            t.Start();
        }
        
        private void openPs()
        {
            Thread.Sleep(500);
            if (wb.InvokeRequired){
                OpenPsCallback setListCallback = new OpenPsCallback(openPs); //实例化委托对象
                wb.Invoke(setListCallback); //重新调用SetListBox函数
            }else {
                getHtml();
                htmlDoc = htmlDoc.Window.Frames["activediv"].Document;
                htmlDoc = htmlDoc.GetElementById("act5").Document;
                for (int i = 0; i < htmlDoc.All.Count; i++)          //循环查找这个对象的每一个元素
                {
                    if (htmlDoc.All[i].TagName.ToLower() == "div")           //如果这个元素是A
                    {
                        HtmlElement myelement = htmlDoc.All[i];       //就把这个元素实例化成一个HtmlElement对象
                        if (myelement.InnerHtml!=null&&myelement.InnerHtml.IndexOf("开始跑商") != -1)       //如果这个元素的文字是“下一页"
                        {
                            myelement.InvokeMember("click");    //对这个元素进行点击
                            Thread t = new Thread(new ThreadStart(getHtml));
                            t.Start();
                        }
                    }
                }
            }
        }
        private void itemClick(string frame,string tag,string html)
        {
            if (wb.InvokeRequired)
            {
                OpenPsCallback setListCallback = new OpenPsCallback(openPs); //实例化委托对象
                tb_output.Invoke(setListCallback); //重新调用SetListBox函数
            }
            else {
                HtmlDocument htmlDoc = wb.Document;
                if(frame!=null)htmlDoc = wb.Document.Window.Frames[frame].Document;
                for (int i = 0; i < htmlDoc.All.Count; i++)          //循环查找这个对象的每一个元素
                {
                    if (htmlDoc.All[i].TagName.ToLower() == tag)           //如果这个元素是A
                    {
                        HtmlElement myelement = htmlDoc.All[i];       //就把这个元素实例化成一个HtmlElement对象
                        if (myelement.InnerHtml != null && myelement.InnerHtml.IndexOf(html) != -1)       //如果这个元素的文字是“下一页"
                        {
                            myelement.InvokeMember("click");    //对这个元素进行点击
                            Thread.Sleep(1000);
                            checkPs();
                        }
                    }
                }
            }
        }
        private void timing(){
            if (isTiming) return;
            isTiming = true;
            System.Timers.Timer t = new System.Timers.Timer(1000);//实例化Timer类，设置间隔时间为10000毫秒；
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件；
            t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
        }
        public void theout(object source, System.Timers.ElapsedEventArgs e){
            count++;
            SetTime(count.ToString());
            if (count % 5 == 0 && cb_beatk.Checked) CheckBeAtk();
            if (count < 180 || !cb_ps.Checked) return;
            count = 0;
            checkPs();
        }
    }
}
